package tothreedesignmodel.simplefactory;

/**
 * @Author lzhphantom
 * @Description
 * @Date 创建于 15:10 2020/7/16
 * @Modified
 */
public class SimpleFactory {
    public static final String BASKETBALL = "basketball";
    public static final String SOCCER = "soccer";

    public static Ball createBall(String name) {
        if (BASKETBALL.equals(name)) {
            return new BasketBall();
        } else if (SOCCER.equals(name)) {
            return new Soccer();
        } else {
            return null;
        }
    }


    /**
     * 利用反射完善简单工厂
     *
     * @param clazz
     * @return java.lang.Object
     * @description
     * @method createBallWithReflect
     * @author lzhphantom
     * @date 17:19 2020/7/20
     */
    public static Object createBallWithReflect(Class<? extends Ball> clazz) {
        Object o = null;
        try {
            o = Class.forName(clazz.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static void main(String[] args) {
        System.out.println(createBallWithReflect(BasketBall.class));
        System.out.println(SimpleFactory.createBall(BASKETBALL));
        System.out.println(SimpleFactory.createBall(SOCCER));
    }
}
